package com.ruoyi.web.controller.gcgl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.ConvertUpMoneyUtil;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.DecimalCalculateUtil;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.gcgl.domain.GcFyhx;
import com.ruoyi.gcgl.domain.GcFyhxitem;
import com.ruoyi.gcgl.mapper.GcSendTipMapper;
import com.ruoyi.gcgl.service.IGcFyhxService;
import com.ruoyi.gcgl.service.IGcFyhxitemService;
import com.ruoyi.gcgl.service.IGcSendTipService;
import com.ruoyi.web.controller.tool.AliyunSmsUtils;

/**
 * 费用核销Controller
 * 
 * @author ruoyi
 * @date 2022-12-17
 */
@Controller
@RequestMapping("/gcgl/fyhx")
public class GcFyhxController extends BaseController
{
    private String prefix = "gcgl/fyhx";

    @Autowired
    private IGcFyhxService gcFyhxService;
    @Autowired
    private IGcFyhxitemService gcFyhxitemService;
    @Autowired
    private IGcSendTipService gcSendTipService;
    @Autowired
    private GcSendTipMapper gcSendTipMapper;
    
    @RequiresPermissions("gcgl:fyhx:view")
    @GetMapping()
    public String fyhx()
    {
        return prefix + "/fyhx";
    }

    /**
     * 查询费用核销列表
     */
    @RequiresPermissions("gcgl:fyhx:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(GcFyhx gcFyhx)
    {
        startPage();
        gcFyhx.setDeleteFlag(1);
        if(getSysUser().isShow43R(getUserId())){
        gcFyhx.setCreateUser(getLoginName());
        }
        List<GcFyhx> list = gcFyhxService.selectGcFyhxList(gcFyhx);
        return getDataTable(list);
    }

    /**
     * 导出费用核销列表
     */
    @RequiresPermissions("gcgl:fyhx:export")
    @Log(title = "费用核销", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(GcFyhx gcFyhx)
    {
    	gcFyhx.setDeleteFlag(1);
    	if(getSysUser().isShow()){
            gcFyhx.setCreateUser(getLoginName());
            }
        List<GcFyhx> list = gcFyhxService.selectGcFyhxList(gcFyhx);
        ExcelUtil<GcFyhx> util = new ExcelUtil<GcFyhx>(GcFyhx.class);
        return util.exportExcel(list, "费用核销数据");
    }

    /**
     * 新增费用核销
     */
    @GetMapping("/add")
    public String add(ModelMap mmap){
    	mmap.put("bxr", getLoginName());
    	mmap.put("bxrAt", DateUtils.getDate());
        return prefix + "/add";
    }

    /**
     * 新增保存费用核销
     */
    @RequiresPermissions("gcgl:fyhx:add")
    @Log(title = "费用核销", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public synchronized AjaxResult addSave(GcFyhx gcFyhx){
    	try {
    		SimpleDateFormat sdy = new SimpleDateFormat("yyyy");
    		SimpleDateFormat sdm = new SimpleDateFormat("MM");
    		int year = Integer.parseInt(sdy.format(new Date()));
    		int month = Integer.parseInt(sdm.format(new Date()));
    		GcFyhx newObj = new GcFyhx();
    		newObj.setDeleteFlag(1);
    		newObj.setCreateAt(DateUtils.getFisrtDayOfMonth(year, month));
    		newObj.setUpdateAt(DateUtils.getLastDayOfMonth(year, month));
    		List<GcFyhx> lisCount = gcFyhxService.selectGcFyhxList(newObj);
    		
    		String no = String.format("%02d", lisCount.size()+1);
    		String l_month = String.format("%02d", month);
    		gcFyhx.setOrderNo(year+"-"+l_month+"-"+no);
    		gcFyhx.setDeleteFlag(1);
        	gcFyhx.setCreateAt(new Date());
        	gcFyhx.setUpdateAt(new Date());
        	gcFyhx.setCreateUser(getLoginName());
        	gcFyhx.setUpdateUser(getLoginName());
        	gcFyhx.setStatus(0);
        	gcFyhx.setBxr(getLoginName());
        	gcFyhx.setBxrAt(new Date());
        	gcFyhxService.insertGcFyhx(gcFyhx);
        	
        	Double hejexx = 0.0;
        	for(GcFyhxitem gcFyhxitem:gcFyhx.getItem()){
        		hejexx=DecimalCalculateUtil.add(hejexx, 
        				Double.parseDouble(gcFyhxitem.getJine().toString()));
        		gcFyhxitem.setDeleteFlag(1);
        		gcFyhxitem.setCreateAt(new Date());
        		gcFyhxitem.setUpdateAt(new Date());
        		gcFyhxitem.setCreateUser(getLoginName());
        		gcFyhxitem.setUpdateUser(getLoginName());
        		gcFyhxitem.setFyhxId(gcFyhx.getId());
        		gcFyhxitemService.insertGcFyhxitem(gcFyhxitem);
        	}
        	
        	gcFyhx.setBxrje(Double.valueOf(hejexx));
        	gcFyhx.setHejexx(hejexx.toString());
        	gcFyhx.setHejedx(ConvertUpMoneyUtil.toChinese(hejexx.toString()));
        	gcFyhxService.updateGcFyhx(gcFyhx);
        	
        	//发送消息通知
//        	gcSendTipService.sendTip("fyhx", gcFyhx.getType(), gcFyhx.getOrderNo(), 
//        			gcFyhx.getCreateUser(), DateUtils.dateTime(gcFyhx.getCreateAt()), 
//        			gcFyhx.getStatus().toString(), null, gcFyhx.getId().toString());
        	
        	try {
//        		List<Map> userlis = gcSendTipMapper.selectGcToUserByName(gcFyhx.getCreateUser());
//        		if(userlis!=null && userlis.size()>0){
//        			String phonenumber = userlis.get(0).get("phonenumber").toString();//创建人
        		String phonenumber = caiw;
        			AliyunSmsUtils.returnSmsTz(phonenumber);
//        		}
			} catch (Exception e) {
				// TODO: handle exception
				e.printStackTrace();
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			return toAjax(0);
		}
    	
        return toAjax(1);
    }

    /**
     * 修改费用核销
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap){
        GcFyhx gcFyhx = gcFyhxService.selectGcFyhxById(id);
        GcFyhxitem gcFyhxitem = new GcFyhxitem();
        gcFyhxitem.setFyhxId(id);
        List<GcFyhxitem> items = gcFyhxitemService.selectGcFyhxitemList(gcFyhxitem);
        gcFyhx.setItem(items);
        mmap.put("gcFyhx", gcFyhx);
        return prefix + "/edit";
    }

    /**
     * 修改保存费用核销
     */
    @Log(title = "费用核销", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(GcFyhx gcFyhx)
    {
    	try {
    		gcFyhxitemService.deleteGcFyhxitemById(gcFyhx.getId());
    		
    		Double hejexx = 0.0;
        	//item
        	for(GcFyhxitem item : gcFyhx.getItem()){
        		hejexx=DecimalCalculateUtil.add(hejexx, 
        				Double.parseDouble(item.getJine().toString()));
        		item.setDeleteFlag(1);
        		item.setUpdateAt(new Date());
        		item.setCreateUser(getLoginName());
        		item.setUpdateUser(getLoginName());
        		item.setFyhxId(gcFyhx.getId());
        		gcFyhxitemService.insertGcFyhxitem(item);
        	}
        	
        	gcFyhx.setBxrje(Double.valueOf(hejexx));
        	gcFyhx.setHejexx(hejexx.toString());
        	gcFyhx.setHejedx(ConvertUpMoneyUtil.toChinese(hejexx.toString()));
        	gcFyhx.setUpdateAt(new Date());
        	gcFyhx.setUpdateUser(getLoginName());
        	gcFyhxService.updateGcFyhx(gcFyhx);
        	
        	try {
//        		List<Map> userlis = gcSendTipMapper.selectGcToUserByName(gcFyhx.getCreateUser());
//        		if(userlis!=null && userlis.size()>0){
//        			String phonenumber = userlis.get(0).get("phonenumber").toString();//创建人
        		String phonenumber = caiw;
        			AliyunSmsUtils.returnSmsTz(phonenumber);
//        		}
			} catch (Exception e) {
				// TODO: handle exception
				e.printStackTrace();
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			return toAjax(0);
		}
    	
        return toAjax(1);
    }

    /**
     * 删除费用核销
     */
    @RequiresPermissions("gcgl:fyhx:remove")
    @Log(title = "费用核销", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(gcFyhxService.deleteGcFyhxByIds(ids));
    }
    
    /////财务审批
    @RequiresPermissions("gcgl:fyhx:cwpsview")
    @GetMapping("/cwps")
    public String cwps()
    {
        return prefix + "/fyhxcwps";
    }
    
    @PostMapping("/cwpslist")
    @ResponseBody
    public TableDataInfo cwpslist(GcFyhx gcFyhx)
    {
        startPage();
        gcFyhx.setDeleteFlag(1);
        gcFyhx.setStatus(1);
        List<GcFyhx> list = gcFyhxService.selectGcFyhxList(gcFyhx);
        return getDataTable(list);
    }
    
    @Log(title = "通过", businessType = BusinessType.UPDATE)
    @PostMapping("/cwyes")
    @ResponseBody
    public AjaxResult cwyes(String ids, ModelMap mmap){
    	String[] id = Convert.toStrArray(ids);
    	for(int i=0;i<id.length;i++){
    		GcFyhx obj = new GcFyhx();
    		obj.setId(Long.parseLong(id[i]));
    		obj.setStatus(3);
    		obj.setSpr(getLoginName());
    		obj.setSprAt(new Date());
    		gcFyhxService.updateGcFyhx(obj);
    		
    		GcFyhx gcFyhx = gcFyhxService.selectGcFyhxById(Long.parseLong(id[i].toString()));
//    		gcSendTipService.sendTip("fyhx", gcFyhx.getType(), gcFyhx.getOrderNo(), 
//        			gcFyhx.getCreateUser(), DateUtils.dateTime(gcFyhx.getCreateAt()), 
//        			gcFyhx.getStatus().toString(), null, gcFyhx.getId().toString());
    		
    		try {
//        		List<Map> userlis = gcSendTipMapper.selectGcToUserByName(gcFyhx.getCreateUser());
//        		if(userlis!=null && userlis.size()>0){
//        			String phonenumber = userlis.get(0).get("phonenumber").toString();//创建人
    			String phonenumber = zjl;
        			AliyunSmsUtils.returnSmsTz(phonenumber);
//        		}
			} catch (Exception e) {
				// TODO: handle exception
				e.printStackTrace();
			}
    	}
    	
    	return toAjax(1);
    }
    
    @Log(title = "不通过", businessType = BusinessType.UPDATE)
    @PostMapping("/cwno")
    @ResponseBody
    public AjaxResult cwno(String ids, ModelMap mmap){
    	String[] id = Convert.toStrArray(ids);
    	for(int i=0;i<id.length;i++){
    		GcFyhx obj = new GcFyhx();
    		obj.setId(Long.parseLong(id[i]));
    		obj.setStatus(4);
    		obj.setSpr(getLoginName());
    		obj.setSprAt(new Date());
    		gcFyhxService.updateGcFyhx(obj);
    		
    		GcFyhx gcFyhx = gcFyhxService.selectGcFyhxById(Long.parseLong(id[i].toString()));
//    		gcSendTipService.sendTip("fyhx", gcFyhx.getType(), gcFyhx.getOrderNo(), 
//        			gcFyhx.getCreateUser(), DateUtils.dateTime(gcFyhx.getCreateAt()), 
//        			gcFyhx.getStatus().toString(), null, gcFyhx.getId().toString());
    		
    		try {
        		List<Map> userlis = gcSendTipMapper.selectGcToUserByName(gcFyhx.getCreateUser());
        		if(userlis!=null && userlis.size()>0){
        			String phonenumber = userlis.get(0).get("phonenumber").toString();//创建人
        			AliyunSmsUtils.returnSmsTz(phonenumber);
        		}
			} catch (Exception e) {
				// TODO: handle exception
				e.printStackTrace();
			}
    	}
    	return toAjax(1);
    }
    
    /////总经理审批
    @RequiresPermissions("gcgl:fyhx:zjlpsview")
    @GetMapping("/zjlps")
    public String zjlps()
    {
        return prefix + "/fyhxzjlps";
    }
    
    @PostMapping("/zjlpslist")
    @ResponseBody
    public TableDataInfo zjlpslist(GcFyhx gcFyhx)
    {
        startPage();
        gcFyhx.setDeleteFlag(1);
        gcFyhx.setStatus(0);
        List<GcFyhx> list = gcFyhxService.selectGcFyhxList(gcFyhx);
        return getDataTable(list);
    }
    
    @Log(title = "通过", businessType = BusinessType.UPDATE)
    @PostMapping("/zjlyes")
    @ResponseBody
    public AjaxResult zjlyes(String ids, ModelMap mmap){
    	String[] id = Convert.toStrArray(ids);
    	for(int i=0;i<id.length;i++){
    		GcFyhx obj = new GcFyhx();
    		obj.setId(Long.parseLong(id[i]));
    		obj.setStatus(1);
    		obj.setLdsp(getLoginName());
    		obj.setLdspAt(new Date());
    		gcFyhxService.updateGcFyhx(obj);
    		
    		GcFyhx gcFyhx = gcFyhxService.selectGcFyhxById(Long.parseLong(id[i].toString()));
//    		gcSendTipService.sendTip("fyhx", gcFyhx.getType(), gcFyhx.getOrderNo(), 
//        			gcFyhx.getCreateUser(), DateUtils.dateTime(gcFyhx.getCreateAt()), 
//        			gcFyhx.getStatus().toString(), null, gcFyhx.getId().toString());
    		
    		try {
        		List<Map> userlis = gcSendTipMapper.selectGcToUserByName(gcFyhx.getCreateUser());
        		if(userlis!=null && userlis.size()>0){
        			String phonenumber = userlis.get(0).get("phonenumber").toString();//创建人
        			AliyunSmsUtils.returnSmsTz(phonenumber);
        		}
			} catch (Exception e) {
				// TODO: handle exception
				e.printStackTrace();
			}
    	}
    	
    	return toAjax(1);
    }
    
    @Log(title = "不通过", businessType = BusinessType.UPDATE)
    @PostMapping("/zjlno")
    @ResponseBody
    public AjaxResult zjlno(String ids, ModelMap mmap){
    	String[] id = Convert.toStrArray(ids);
    	for(int i=0;i<id.length;i++){
    		GcFyhx obj = new GcFyhx();
    		obj.setId(Long.parseLong(id[i]));
    		obj.setStatus(2);
    		obj.setLdsp(getLoginName());
    		obj.setLdspAt(new Date());
    		gcFyhxService.updateGcFyhx(obj);
    		
    		GcFyhx gcFyhx = gcFyhxService.selectGcFyhxById(Long.parseLong(id[i].toString()));
//    		gcSendTipService.sendTip("fyhx", gcFyhx.getType(), gcFyhx.getOrderNo(), 
//        			gcFyhx.getCreateUser(), DateUtils.dateTime(gcFyhx.getCreateAt()), 
//        			gcFyhx.getStatus().toString(), null, gcFyhx.getId().toString());
    		
    		try {
        		List<Map> userlis = gcSendTipMapper.selectGcToUserByName(gcFyhx.getCreateUser());
        		if(userlis!=null && userlis.size()>0){
        			String phonenumber = userlis.get(0).get("phonenumber").toString();//创建人
        			AliyunSmsUtils.returnSmsTz(phonenumber);
        		}
			} catch (Exception e) {
				// TODO: handle exception
				e.printStackTrace();
			}
    	}
    	return toAjax(1);
    }
    
    @GetMapping("/fyhxprint/{id}")
    public String fyhxprint(@PathVariable("id") Long id, ModelMap mmap){
    	GcFyhx obj = gcFyhxService.selectGcFyhxById(id);
        
    	GcFyhxitem items = new GcFyhxitem();
        items.setFyhxId(id);
        List<GcFyhxitem> itemlis= gcFyhxitemService.selectGcFyhxitemList(items);
        mmap.put("count", itemlis.size());
        if(itemlis!=null){
        	obj.setItem(itemlis);
        }
        mmap.put("obj", obj);
        
        return prefix + "/fyhxprint";
    }
    
    @GetMapping("/fyhxcwsp/{id}")
    public String fyhxcwsp(@PathVariable("id") Long id, ModelMap mmap){
    	GcFyhx obj = gcFyhxService.selectGcFyhxById(id);
        
    	GcFyhxitem items = new GcFyhxitem();
        items.setFyhxId(id);
        List<GcFyhxitem> itemlis= gcFyhxitemService.selectGcFyhxitemList(items);
        mmap.put("count", itemlis.size());
        if(itemlis!=null){
        	obj.setItem(itemlis);
        }
        mmap.put("obj", obj);
        
        return prefix + "/fyhxcwsp";
    }
    
    @GetMapping("/fyhxzjlsp/{id}")
    public String fyhxzjlsp(@PathVariable("id") Long id, ModelMap mmap){
    	GcFyhx obj = gcFyhxService.selectGcFyhxById(id);
        
    	GcFyhxitem items = new GcFyhxitem();
        items.setFyhxId(id);
        List<GcFyhxitem> itemlis= gcFyhxitemService.selectGcFyhxitemList(items);
        mmap.put("count", itemlis.size());
        if(itemlis!=null){
        	obj.setItem(itemlis);
        }
        mmap.put("obj", obj);
        
        return prefix + "/fyhxzjlsp";
    }
}